package com.tianma315.admin.roottrace.view;

import com.alibaba.fastjson.JSONObject;
import com.tianma315.admin.roottrace.service.RootTraceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/rootTrace/view")
public class RootTraceController {

    @Autowired
    private RootTraceService rootTraceService;

    @RequestMapping("/search")
    public String toSearchView(){
        return "/root_trace/search";
    }

    @PostMapping("/result")
    public ModelAndView toResult(@RequestParam("batch") String batch,
                           @RequestParam("productName")String productName){
        ModelAndView modelAndView = new ModelAndView("/root_trace/result");
        JSONObject jsonObject = rootTraceService.getTraceInfo(batch,productName);
        modelAndView.addObject("result",jsonObject);
        return modelAndView;
    }

    @ResponseBody
    @PostMapping("/getData")
    public JSONObject getData(@RequestParam("batch") String batch,
                                 @RequestParam("productName")String productName){
        JSONObject jsonObject = rootTraceService.getTraceInfo(batch,productName);
        return jsonObject;
    }

}
